import requests
from requests.adapters import HTTPAdapter

from common.yaml_config import GetConf
from common.deal_with_response import deal_with_res


class Requests:
    def __init__(self, headers=None, timeout=10):
        """
        requests封装
        Args:
            timeout: 每个请求的超时时间
        """
        self.s = requests.Session()
        # 在session实例上挂载Adapter实例, 目的: 请求异常时,自动重试
        self.s.mount("http://", HTTPAdapter(max_retries=3))
        self.s.mount("https://", HTTPAdapter(max_retries=3))

        # 公共的请求头设置
        self.s.headers = headers
        self.timeout = timeout
        self.url = GetConf().get_url()

    def get(self, url, params=None):
        """
        GET
        Args:
            url: 接口地址
            params: 一般GET的参数都是放在URL查询参数里面

        Returns:

        """
        res = self.s.get(self.url + url, params=params, timeout=self.timeout)
        deal_with_res(params, res)
        return res

    def post(self, url, data=None, json=None):
        """
        POST
        Args:
            url:接口地址
            data: 参数放在表单参数中
            json: 参数放在请求体中(这时候 Content-Type: application/json )

        Returns:

        """
        if data:
            res = self.s.post(self.url + url, data=data, timeout=self.timeout)
            deal_with_res(data, res)
            return res
        if json:
            res = self.s.post(self.url + url, json=json, timeout=self.timeout)
            deal_with_res(json, res)
            return res
        res = self.s.post(self.url + url, timeout=self.timeout)
        deal_with_res(json, res)
        return res

    def __del__(self):
        """
        当实例被销毁时,释放掉session所持有的连接
        Returns:

        """
        if self.s:
            self.s.close()
